var database = require('../../data/data.js');
Page({

  data: {
    people: 0
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    var that = this;
    this.setData({
      identity: options.identity,
      openid: options.popenid,
      id: options.gameid,
    })
    wx.request({
      url: database.url + '/project/getContestDetail.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        openid: options.popenid,
        id: options.gameid,
      },
      success(res) {
        var baoming = 0;
        var daiding = 0;
        var qingjia = 0;
        for (var i = 0; i < res.data.players.length; i++) {
          if (res.data.players[i].playerStatus == "报名") {
            baoming++;
          } else if (res.data.players[i].playerStatus == "待定") {
            daiding++;
          } else {
            qingjia++;
          }
        }
        that.setData({
          players: res.data.players,
          playerslength: res.data.players.length,
          baoming: baoming,
          daiding: daiding,
          qingjia: qingjia
        })
        var contest = res.data.contest;
        var date = new Date(contest.postdate);
        var date1 = new Date();
        var seconds = 0;
        var minutes = 0;
        var hour = 0;
        var day = 0;
        var month = 0;
        var year = 0;
        var timeDifference = "";
        var contact = contest.contact;
        contact = contact.substring(0, 3) + "****" + contact.substring(7);
        if ((date1 - date) / 1000 >= 60) {
          if ((date1 - date) / 1000 / 60 >= 60) {
            if ((date1 - date) / 1000 / 60 / 60 >= 24) {
              if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                  year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                  timeDifference = year + "年前";
                } else {
                  month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                  timeDifference = month + "个月前";
                }
              } else {
                day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                timeDifference = day + "天前";
              }
            } else {
              hour = parseInt((date1 - date) / 1000 / 60 / 60);
              timeDifference = hour + "小时前";
            }
          } else {
            minutes = parseInt((date1 - date) / 1000 / 60);
            timeDifference = minutes + "分钟前·";
          }
        } else {
          seconds = parseInt((date1 - date) / 1000);
          timeDifference = seconds + "秒前";
        };
        wx.request({
          url: database.url1 + '/api/V2/user/access_token',
          data: {
            clientId: "QqKTO5BTPN6REI26QAgq",
            clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
          },
          success(res) {
            database.accessToken = res.data.data.accessToken;
            wx.request({
              url: database.url1 + '/api/V2/softball/get_softball',
              data: {
                accessToken: database.accessToken,
                userId: contest.popenid,
                softballId: contest.pteamid
              },
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success(res) {
                that.setData({
                  team1: res.data.data
                });
              }
            });
            wx.request({
              url: database.url1 + '/api/V2/softball/get_softball',
              data: {
                accessToken: database.accessToken,
                userId: contest.mopenid,
                softballId: contest.mteamid
              },
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success(res) {
                wx.hideLoading();
                that.setData({
                  timeDifference: timeDifference,
                  data: contest,
                  contact: contact,
                  team2: res.data.data
                });
              }
            });
          }
        });
      }
    })
  },
  notesChange: function (e) {
    if (e.detail.value != "") {
      this.setData({
        notes: e.detail.value
      })
    }
  },
  sliderchange: function (e) {
    this.setData({
      people: e.detail.value
    })
  },
  showLocation: function () {
    var that = this;
    wx.openLocation({
      latitude: parseInt(that.data.data.lat),
      longitude: parseInt(that.data.data.lng),
    })
  },
  join: function () {
    var that = this;
    var itemList = ["报名", "待定", "请假"];
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        var status = itemList[res.tapIndex];
        var code = '';
        var openid = '';
        var nickName = '';
        var avatarUrl = '';
        wx.login({
          success(res) {
            database.code = res.code;
            wx.request({
              url: database.url + '/api/V2/integral/integral_openid',
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                code: database.code,
                accessToken: database.accessToken,
                weChatId: "woaizuqiuyuezhanguangchang",
                regionName: "other"
              },
              success(res) {
                var sessoionKey = res.data.data.session_key;
                wx.getUserInfo({
                  data: {
                    withCredentials: true,
                    lang: "zh_CN"
                  },
                  success(res) {
                    wx.request({
                      url: database.url + '/api/V2/user/we_chat_user',
                      method: "POST",
                      header: {
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        accessToken: database.accessToken,
                        weChatId: "woaizuqiuyuezhanguangchang",
                        encryptedData: res.encryptedData,
                        iv: res.iv,
                        sessionKey: sessoionKey,
                        weChatName: "我爱足球约战广场"
                      },
                      success(res) {
                        openid = res.data.data.unionId;
                        wx.getUserInfo({
                          success(e) {
                            wx.request({
                              url: database.url + '/project/uploadNewPlayer.action',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
                              },
                              data: {
                                openid: openid,
                                person: that.data.data.person,
                                matchid: that.data.data.gameid,
                                nickName: e.userInfo.nickName,
                                avatarUrl: e.userInfo.avatarUrl,
                                notes: that.data.notes,
                                identity: that.data.identity,
                                status: status,
                              },
                              method: "POST",
                              header: {
                                'content-type': 'application/x-www-form-urlencoded'
                              },
                              success(res) {
                                if (res.data == 0) {
                                  wx.showToast({
                                    title: '参加比赛成功!',
                                  })
                                } else {
                                  wx.showToast({
                                    title: '你已参加比赛!',
                                  })
                                }
                                wx.redirectTo({
                                  url: "/pages/join_game/join_game?identity=" + that.data.data.identity + "&popenid=" + that.data.data.popenid + "&gameid=" + that.data.data.gameid,
                                })
                              }
                            });
                          }
                        })
                      }
                    })
                  }
                })
              }
            })
          }
        })
      },
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: "比赛即将开始，队友们快来报名",
      path: "/pages/join_game/join_game?identity=" + this.data.data.identity + "&popenid=" + this.data.data.popenid + "&gameid=" + this.data.data.gameid,
      complete: function (res) {
      },
    }
  }
})